package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dao.CategoryMapper;
import com.itheima.reggie.dao.DishMapper;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
   @Autowired
    DishFlavorService dishFlavorService;

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        //获取菜品与口味的关系
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
         //保存菜品口味数据到口味表中
        dishFlavorService.saveBatch(flavors);

    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品信息
        Dish dish = this.getById(id);
        //查询flavor
        DishDto dishDto = new DishDto();

        LambdaQueryWrapper<DishFlavor> queryWrapper = Wrappers.lambdaQuery(DishFlavor.class)
                .eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper);
        //装填菜品与flavor
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(dishFlavorList);

        return dishDto;
    }

    @Override
    @Transactional
    public DishDto updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);

        //先移除数据库中,菜品原有所有风味,避免和新传入的数据判断,麻烦
        LambdaQueryWrapper<DishFlavor> queryWrapper = Wrappers.lambdaQuery(DishFlavor.class)
                .eq(DishFlavor::getDishId, dishDto.getId());
       dishFlavorService.remove(queryWrapper);

       //前端携带的数据,给新增的风味绑定所属菜品
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));

      dishFlavorService.saveBatch(flavors);
        return dishDto;
    }

    @Override
    @Transactional
    //待修改
    public void deleteWithFlavor(Long[] ids) {
        //判断
        for (Long id : ids) {
            this.removeById(id);
            //获取菜品与口味的关系
            LambdaQueryWrapper<DishFlavor> queryWrapper = Wrappers.lambdaQuery(DishFlavor.class).eq(DishFlavor::getDishId, id);
            dishFlavorService.remove(queryWrapper);
        }
    }

    @Override
    public void updateStatus(Integer status, Long[] ids) {
        //update dish set status = #{status} where id in (ids)
        LambdaQueryWrapper<Dish> queryWrapper = Wrappers.lambdaQuery(Dish.class)
                .in(Dish::getId, Arrays.asList(ids));
        List<Dish> list = this.list(queryWrapper);
        list.forEach(dish -> dish.setStatus(status));
        this.updateBatchById(list);
    }
}
